package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.BuildingDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.BuildingEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.BuildingItemVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
public class BuildingManager extends BaseManager {
    private final BuildingDAO buildingDAO;

    public BuildingManager(BuildingDAO buildingDAO) {
        this.buildingDAO = buildingDAO;
    }

    public Integer getPageCount(String name) {
        int count = buildingDAO.count(name);
        return calcPageCount(count, BuildingDAO.PAGE_SIZE);
    }

    @Transactional
    public List<BuildingItemVO> getPage(Integer index, String namePart) {
        List<BuildingItemVO> buildingItemList = new ArrayList<>();
        List<BuildingEntity> buildingList = buildingDAO.getPage(index, namePart);

        for (BuildingEntity building : buildingList) {
            int id = building.getId();
            String name = building.getName();
            String remarks=building.getRemarks();
            buildingItemList.add(new BuildingItemVO(id, name,remarks));
        }

        return buildingItemList;
    }

    public BuildingEntity get(Integer id) {
        return buildingDAO.get(id);
    }

    public int create(BuildingEntity entity) {
        return buildingDAO.insert(entity);
    }

    public int update(BuildingEntity entity) {
        return buildingDAO.update(entity);
    }

    public int delete(Integer id) {
        return buildingDAO.delete(id);
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<BuildingEntity> entityList = buildingDAO.listName();
        for (BuildingEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName(),entity.getRemarks()));
        }

        return voList;
    }
}
